<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .login-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            padding: 2.5rem;
            width: 100%;
            max-width: 400px;
            backdrop-filter: blur(10px);
        }
        
        .login-title {
            text-align: center;
            font-weight: 700;
            color: #333;
            margin-bottom: 2rem;
        }
        
        .form-control {
            border: none;
            border-bottom: 2px solid #ddd;
            border-radius: 0;
            padding: 0.75rem 0;
            background: transparent;
            transition: border-color 0.3s ease;
        }
        
        .form-control:focus {
            box-shadow: none;
            border-bottom-color: #667eea;
            background: transparent;
        }
        
        .btn-login {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 25px;
            padding: 0.75rem 2rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
        }
        
        .alert {
            border-radius: 10px;
            border: none;
        }
        
        .form-floating {
            margin-bottom: 1.5rem;
        }
        
        .form-floating > label {
            color: #666;
        }
    </style>
</head>
<body>
    <div class="login-card">
        <h2 class="login-title">管理员登录</h2>
        
        <div id="alert-container"></div>
        
        <form id="loginForm">
            <div class="form-floating">
                <input type="text" class="form-control" id="username" name="username" required>
                <label for="username">用户名</label>
            </div>
            
            <div class="form-floating">
                <input type="password" class="form-control" id="password" name="password" required>
                <label for="password">密码</label>
            </div>
            
            <div class="d-grid">
                <button type="submit" class="btn btn-primary btn-login">
                    <span id="login-text">登录</span>
                    <span id="login-spinner" class="spinner-border spinner-border-sm d-none ms-2"></span>
                </button>
            </div>
        </form>
        
        <div class="text-center mt-4">
            <small class="text-muted">
                默认用户名: admin, 密码: admin123
            </small>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const submitBtn = e.target.querySelector('button[type="submit"]');
            const loginText = document.getElementById('login-text');
            const loginSpinner = document.getElementById('login-spinner');
            const alertContainer = document.getElementById('alert-container');
            
            // 显示加载状态
            submitBtn.disabled = true;
            loginText.textContent = '登录中...';
            loginSpinner.classList.remove('d-none');
            
            const formData = new FormData(e.target);
            const data = Object.fromEntries(formData);
            
            try {
                const response = await fetch('/admin/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showAlert('success', '登录成功，正在跳转...');
                    setTimeout(() => {
                        window.location.href = '/admin';
                    }, 1000);
                } else {
                    showAlert('danger', result.message);
                    resetButton();
                }
            } catch (error) {
                showAlert('danger', '登录失败，请检查网络连接');
                resetButton();
            }
            
            function resetButton() {
                submitBtn.disabled = false;
                loginText.textContent = '登录';
                loginSpinner.classList.add('d-none');
            }
        });
        
        function showAlert(type, message) {
            const alertContainer = document.getElementById('alert-container');
            alertContainer.innerHTML = `
                <div class="alert alert-${type} alert-dismissible fade show" role="alert">
                    ${message}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            `;
        }
        
        // 自动聚焦到用户名输入框
        document.getElementById('username').focus();
    </script>
</body>
</html>